package com.example.xd.model_video.videoeditlib.editVideoPlay;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.os.Environment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * ================================================
 * 作    者：顾修忠-guxiuzhong@youku.com/gfj19900401@163.com
 * 版    本：
 * 创建日期：2017/4/4-下午6:56
 * 描    述：
 * 修订历史：
 * ================================================
 */

public class PictureUtils {
    public static final String POSTFIX = ".jpeg";
    private static final String EDIT_PATH = "/colorshow/keyframes/";

    public static String saveImageToSD(Bitmap bmp, String dirPath) {
        if (bmp == null) {
            return "";
        }
        File appDir = new File(dirPath);
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        String fileName = System.currentTimeMillis() + ".jpg";
        File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file.getAbsolutePath();
    }



    public static String saveImageToSDForEdit(Bitmap bmp, String dirPath, String fileName) {
        if (bmp == null) {
            return "";
        }
        File appDir = new File(dirPath);
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 80, fos);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file.getAbsolutePath();
    }

    public static void deleteFile(File f) {
        if (f.isDirectory()) {
            File[] files = f.listFiles();
            if (files != null && files.length > 0) {
                for (int i = 0; i < files.length; ++i) {
                    deleteFile(files[i]);
                }
            }
        }
        f.delete();
    }

    public static String getSaveEditThumbnailDir(Context context) {
        String state = Environment.getExternalStorageState();
        File rootDir = state.equals(Environment.MEDIA_MOUNTED) ? Environment.getExternalStorageDirectory() : context.getCacheDir();
        File folderDir = new File(rootDir.getAbsolutePath() + EDIT_PATH);
        if (!folderDir.exists() && folderDir.mkdirs()) {

        }
        return folderDir.getAbsolutePath();
    }

    public static Bitmap reSize(Bitmap bitmap, int toWidth, int toHeight){
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int resizeWidth, resizeHeight, x, y;
        float rate = (float) (1.0 * toWidth / toHeight);
        if(1.0* width / height == rate){
            x = 0;
            y = 0;
            resizeWidth = width;
            resizeHeight = height;
        } else if(1.0* width / height > rate){
            resizeHeight = height;
            resizeWidth = (int) (height * rate);
            x = (width - resizeWidth) / 2;
            y = 0;
        } else {
            resizeWidth = width;
            resizeHeight = (int) (width / rate);
            x = 0;
            y = (height - resizeHeight) / 2;
        }
        Bitmap b = Bitmap.createBitmap(bitmap, x, y, resizeWidth, resizeHeight);
        if(resizeHeight == toHeight){
            return b;
        } else {
            Matrix matrix = new Matrix();
            matrix.preScale((float) 1.0 * toWidth / resizeWidth, (float) 1.0 * toHeight / resizeHeight);
            return Bitmap.createBitmap(b, 0, 0, resizeWidth, resizeHeight, matrix, false);
        }
    }

    public interface GlideLoadBitmapCallback{
        void getBitmapCallback(Bitmap bitmap);
        void getBitmapFailed(Exception e);
    }

    public static void imgUrlToBitmap(Context context, String url, final GlideLoadBitmapCallback callback) {
        Glide.with(context)
                .load(url)
                .asBitmap()
                .centerCrop()
//                .override(100, 100)
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
                        callback.getBitmapCallback(bitmap);
                    }

                    @Override
                    public void onLoadFailed(Exception e, Drawable errorDrawable) {
                        super.onLoadFailed(e, errorDrawable);
                        callback.getBitmapFailed(e);
                    }
                });
    }

}
